package com.hq.petmap;

import java.util.ArrayList;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.hq.petmap.model.Pet;
import com.hq.petmap.task.HQTask;
import com.hq.petmap.task.HQTaskAdapter;
import com.hq.petmap.task.HQTaskParams;
import com.hq.petmap.task.HQTaskResult;
import com.hq.petmap.task.HQTaskResult.HQTaskStatus;

public class NearbyActivity extends Activity {
	
	ListView list;
	NearbyListAdapter adapter;
	View footer;
	View more;
	TextView progressText;
	TextView moreTextFooter;
	ProgressBar moreProgressBarFooter;
	ArrayList<Pet> allPetList;
	LoadMorePetsTask loadMorePets;
	LoadMorePetsTaskAdapter loadPetsAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.nearby_list);
		
		list= (ListView) findViewById(R.id.list);
		more = View.inflate(this, R.layout.listview_footer, null);
		list.addFooterView(more, null, true);
		moreTextFooter = (TextView) findViewById(R.id.ask_for_more);
		moreProgressBarFooter = (ProgressBar) findViewById(R.id.rectangleProgressBar);
		
		list.setOnItemClickListener(mOnClickListener);
		
		allPetList = new ArrayList<Pet>();
		allPetList.add(new Pet("Android", null, "http://50.116.13.60/music/images/adele.png", null, null));

		adapter = new NearbyListAdapter(this, allPetList);
		list.setAdapter(adapter);
		
		//get users
		LoadPetsTask loadPets = new LoadPetsTask();
		LoadPetsTaskAdapter loadPetsAdapter = new LoadPetsTaskAdapter();
		loadPets.setTaskAdapter(loadPetsAdapter);
		loadPets.execute();
	}

	
	protected void onListItemClick(ListView l, View v, int position, long id) {
		if(id == -1 && position >= adapter.getCount()){
			
			if (loadMorePets == null) {
				loadMorePets = new LoadMorePetsTask();
				loadPetsAdapter = new LoadMorePetsTaskAdapter();
				loadMorePets.setTaskAdapter(loadPetsAdapter);
			} 
			if( loadMorePets.getStatus() != AsyncTask.Status.RUNNING) {
				loadMorePets.execute();	
			}	
		}	
	}
	
    private AdapterView.OnItemClickListener mOnClickListener = new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id)
        {
            onListItemClick((ListView)parent, v, position, id);
        }
    };
	
	private class LoadPetsTask extends HQTask {

		@Override
		protected HQTaskResult run(HQTaskParams... params) {
			HQTaskResult result = new HQTaskResult();
			SystemClock.sleep(5000);
			
			ArrayList<Pet> petList = new ArrayList<Pet>();
			petList.add(new Pet("iOS", null, "http://50.116.13.60/music/images/eminem.png", null, null));
			petList.add(new Pet("Blackberry", null, "http://50.116.13.60/music/images/mj.png", null, null));
		
			result.setSatus(HQTaskStatus.OK);
			result.putValue("Pets", petList);
			return result;
		}		
	}
	
	private class LoadPetsTaskAdapter implements HQTaskAdapter {

		@Override
		public void onPreExecute(HQTask task) {
			if(moreTextFooter != null) {
				moreTextFooter.setText("Loading");				
			}
			if(moreProgressBarFooter != null) {
				moreProgressBarFooter.setVisibility(View.VISIBLE);		
			}	
		}

		@SuppressWarnings("unchecked")
		@Override
		public void onPostExecute(HQTask task, HQTaskResult result) {
			if(result.getSatus() == HQTaskStatus.OK) {
				Object value = result.getValue("Pets");
				if(value != null && value instanceof ArrayList){
					ArrayList<Pet> petList = (ArrayList<Pet>) value;
					adapter.addPetsList(petList);
					adapter.notifyDataSetChanged();
				}
			}
			if(moreTextFooter != null) {
				moreTextFooter.setText("More");				
			}
			if(moreProgressBarFooter != null) {
				moreProgressBarFooter.setVisibility(View.INVISIBLE);		
			}
		}

		@Override
		public void onProgressUpdate(HQTask task, Object param) {
			return;			
		}

		@Override
		public void onCancelled(HQTask task) {
			return;						
		}		
	}
	
	private class LoadMorePetsTask extends HQTask {

		@Override
		protected HQTaskResult run(HQTaskParams... params) {
			HQTaskResult result = new HQTaskResult();
			
			SystemClock.sleep(5000);
			
			ArrayList<Pet> petList = new ArrayList<Pet>();
			petList.add(new Pet("iOS", null, "http://50.116.13.60/music/images/eminem.png", null, null));
			
			result.setSatus(HQTaskStatus.OK);
			result.putValue("Pets", petList);
			return result;
		}		
	}
	
	private class LoadMorePetsTaskAdapter implements HQTaskAdapter {

		@Override
		public void onPreExecute(HQTask task) {
			if(moreTextFooter != null) {
				moreTextFooter.setText("Loading");				
			}
			if(moreProgressBarFooter != null) {
				moreProgressBarFooter.setVisibility(View.VISIBLE);		
			}	
		}

		@SuppressWarnings("unchecked")
		@Override
		public void onPostExecute(HQTask task, HQTaskResult result) {
			if(result.getSatus() == HQTaskStatus.OK) {
				Object value = result.getValue("Pets");
				if(value != null && value instanceof ArrayList){
					ArrayList<Pet> petList = (ArrayList<Pet>) value;
					adapter.addPetsList(petList);
					adapter.notifyDataSetChanged();
				}
			}
			if(moreTextFooter != null) {
				moreTextFooter.setText("More");				
			}
			if(moreProgressBarFooter != null) {
				moreProgressBarFooter.setVisibility(View.INVISIBLE);		
			}
		}

		@Override
		public void onProgressUpdate(HQTask task, Object param) {
			return;			
		}

		@Override
		public void onCancelled(HQTask task) {
			return;						
		}		
	}
}

